package com.ks.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author pks
 * @version 2020年2月20日 
 */
public class SUtilIdcardEncry {
	
	private static Map<String, String> encode_map = new HashMap<String, String>();
	private static Map<String, String> decode_map = new HashMap<String, String>();
	static{
		encode_map.put("0", "a");
		encode_map.put("1", "f");
		encode_map.put("2", "d");
		encode_map.put("3", "c");
		encode_map.put("4", "e");
		encode_map.put("5", "3");
		encode_map.put("6", "l");
		encode_map.put("7", "t");
		encode_map.put("8", "5");
		encode_map.put("9", "o");
		
		decode_map.put("a","0");
		decode_map.put("f","1");
		decode_map.put("d","2");
		decode_map.put("c","3");
		decode_map.put("e","4");
		decode_map.put("3","5");
		decode_map.put("l","6");
		decode_map.put("t","7");
		decode_map.put("5","8");
		decode_map.put("o","9" );
	}
	
	public static String encodeIdcard(String idcard){
		StringBuffer sb = new StringBuffer();
		char[] str = idcard.toCharArray();
		for (int i = 0; i < str.length; i++) {
			if(encode_map.containsKey(String.valueOf(str[i]))){
				sb.append(encode_map.get(String.valueOf(str[i])));
			}else{
				sb.append(String.valueOf(str[i]));
			}
		}
		String temp = sb.toString();
		return temp.substring(7,temp.length()) + temp.substring(0, 7) ;
	}
	
	public static String decodeIdcard(String s){
		String temp = s.substring(s.length()-7, s.length()) + s.substring(0,s.length()-7);
		StringBuffer sb = new StringBuffer();
		char[] str = temp.toCharArray();
		for (int i = 0; i < str.length; i++) {
			if(decode_map.containsKey(String.valueOf(str[i]))){
				sb.append(decode_map.get(String.valueOf(str[i])));
			}else{
				sb.append(String.valueOf(str[i]));
			}
		}
		return sb.toString();
	}
	
}
